package com.qdlc.p2p.biz.core.tender.impl;

import java.text.MessageFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.core.tender.AbstractStrategy;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.common.constant.BorrowType;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BorrowModel;

/**
 * 担保标
 * 普通标: 可使用红包 + 加息券
 *
 * @author wuaj
 */
@Service
public class OffVouch extends AbstractStrategy {

    @Resource
    @Qualifier("borrowTenderCollectService")
    private CollectPayService borrowTenderCollectService;

	@Resource
	@Qualifier("borrowFullVerifyPayService")
	private CollectPayService borrowFullVerifyPayService;

	@Resource
	@Qualifier("borrowFullVerifyCollectService")
	private CollectPayService borrowFullVerifyCollectService;

    public int getStrategyType() {
        return BorrowType.TYPE_OFFVOUCH;// 担保标
    }

    @Override
    public TppTradeLog createTenderOrder(BorrowModel borrowModel, User tenderUser) {
        return borrowTenderCollectService.createOrder(borrowModel, tenderUser);
    }

    @Override
    public ReturnResult tender(TppTradeLog tppTradeLog, User tenderUser) {
        logger.info(MessageFormat.format("用户:{0} 正在投标 订单号:{1}", tenderUser.getRealName(), tppTradeLog.getOrderNo()));
        ReturnResult returnResult = borrowTenderCollectService.requestOrder(tppTradeLog);
        logger.info(MessageFormat.format("用户:{0} 提交投标订单号:{1} 成功", tenderUser.getRealName(), tppTradeLog.getOrderNo()));
        return returnResult;
    }

    @Override
    public TppTradeLog createLoanPayOrder(BorrowModel borrowModel) {
		return borrowFullVerifyPayService.createOrder(borrowModel);
    }

    @Override
    public List<TppTradeLog> createRefundCollectOrder(BorrowModel borrowModel) {
        return borrowFullVerifyCollectService.createOrders(borrowModel);
    }

    @Override
    public ReturnResult verifyFullFail(List<TppTradeLog> tppTradeLogList) {
		if (CollectionUtils.isNotNullOrEmpty(tppTradeLogList)) {
			for (TppTradeLog tppTradeLog : tppTradeLogList) {
				borrowFullVerifyCollectService.requestOrder(tppTradeLog);
			}
		}
        return ReturnResult.SUCCESS;
    }

    @Override
    public ReturnResult verifyFullSuccess(TppTradeLog tppTradeLog) {
		logger.info(MessageFormat.format("满标复审 开始复审 订单号:{0}", tppTradeLog.getOrderNo()));
		ReturnResult returnResult = borrowFullVerifyPayService.requestOrder(tppTradeLog);
		logger.info(MessageFormat.format("满标复审 提交投标订单号:{0} 成功", tppTradeLog.getOrderNo()));
		return returnResult;
    }

    @Override
    public ReturnResult cancelBorrow(List<TppTradeLog> tppTradeLogList) {
        if (CollectionUtils.isNotNullOrEmpty(tppTradeLogList)) {
            for (TppTradeLog tppTradeLog : tppTradeLogList) {
                borrowFullVerifyCollectService.requestOrder(tppTradeLog);
            }
        }
        return ReturnResult.SUCCESS;
    }

}